package com.project.purchase.Controller;

import com.project.purchase.Service.*;
import com.project.purchase.entity.User;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author VX5
 * @Title: QZP
 * @ProjectName purchase
 * @Description: TODO
 * @date ${DAT}10:35
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    IUser iUser;

    @Autowired
    IGoods iGoods;

    @Autowired
    Iindent iindent;

    @Autowired
    IProducer iProducer;

     @Autowired
     IPerson iPerson;

    /**
     * 验证登陆信息
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/doLogin")
    public String checkLogin(HttpServletRequest request)throws Exception{
        String name = request.getParameter("userName");
        String password = request.getParameter("pwd");
        List list2 = iGoods.GetAll();
        List list1 = iindent.GetAllIn();
        request.setAttribute("msg1",list1);
        request.setAttribute("msg2",list2);
        User user = iUser.getUserByName(name);
        HttpSession session = request.getSession();
        if (user.getParam().equals("producer")){
            String names = iProducer.GetProIDByLoginName(user.getLoginname());
            session.setAttribute("names",names);
        }else if (user.getParam().equals("user")){
            session.setAttribute("names","非商户");
        }
        if (user != null){
            if (user.getPassword().equals(password)){
                session.setAttribute("user",user);
                return "home";
            }
            else {
                return "error2";
            }
        }else {
            return "error2";
        }
    }

    /**
     * 返回登录界面
     * @return
     * @throws Exception
     */
    @RequestMapping("/back")
    public String backLogin()throws Exception{
        return "login";
    }
}
